/**
 * @ClassName chapter1.KP1_weiyusuan
 * @Description: TODO
 * @Author: JCC
 * @CreateTime: 2023/10/6 17:55
 */

/**
 * 阶乘相加 1！+ 2！+ 3！+ 4！....
 */
public class KP2_jiecheng {
    /**
     * 传统方法实现
     */
    public static int factorial(int num) {
        int sum = 0;
        for (int i = 1; i <= num; i++) {
            int mul = 1;
            for (int j = 1; j <= i; j++) {
                mul *= j;
            }
            sum += mul;
        }
        return sum;
    }

    /**
     * 优化算法
     */
    public static int factorial_2(int num) {
        int sum = 0;
        int mul = 1;
        for (int i = 1; i <= num; i++) {
            mul *= i;
            sum += mul;
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(factorial(1));
        System.out.println(factorial_2(1));
        System.out.println("==================");
        System.out.println(factorial(2));
        System.out.println(factorial_2(2));
        System.out.println("==================");
        System.out.println(factorial(3));
        System.out.println(factorial_2(3));
        System.out.println("==================");
        System.out.println(factorial(4));
        System.out.println(factorial_2(4));
    }
}
